<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/30 0030
 * Time: 上午 9:41
 */

namespace App\Http\Classes;
use Illuminate\Support\Facades\Redis;

class PhoneAli
{
    private $phone;
    private $code;

    protected $host = 'http://sms.market.alicloudapi.com';
    protected $api = '/singleSendSms';
    protected $APPCODE = '339cf34e56ec48c4b8414319e3806181';
    protected $SignName = '英语学习';

    protected $RegisterTemplateCode = 'SMS_100880029';
    protected $ResetTemplateCode = '';

    private $isLock = false;

    private $prefix = [
        'register' => 'p_register_',
        'reset'    => 'p_reset_',
        'lock'     => 'p_lock_',
    ];

    private $lockKey;
    private $registerKey;
    private $resetKey;

    private $Response;

    public function __construct($phone, $isValid = false)
    {
        $this->phone = $phone;
        $this->setKeys();
        if ( $isValid )
        {
            $this->validatorLock();
        }
    }

    private function setKeys()
    {
        $this->lockKey      = $this->prefix['lock'] . $this->phone;
        $this->registerKey  = $this->prefix['register'] . $this->phone;
        $this->resetKey     = $this->prefix['reset'] . $this->phone;
    }

    private function validatorLock()
    {
        if ( Redis::exists( $this->lockKey ) )
        {
            $this->isLock = true;
        }
        else
        {
            $this->isLock = false;
        }
    }

    private function getRegisterCode()
    {
        $this->code = Redis::get($this->registerKey);
    }

    private function getResetCode()
    {
        $this->code = Redis::get($this->resetKey);
    }

    private function createCode()
    {
        $this->code = Common::getRand();
    }

    public function validatorRegisterCode( $code )
    {
        $this->getRegisterCode();
        if ( $code == $this->code )
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public function validatorResetCode( $code )
    {
        $this->getResetCode();
        if ( $code == $this->code )
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    private function addLock()
    {
        Redis::set( $this->lockKey, true );
        Redis::expire( $this->lockKey, 60 );
    }

    public function getPhone()
    {
        return $this->phone;
    }

    public function getLock()
    {
        return $this->isLock;
    }

    private function saveRegisterCode()
    {
        Redis::set( $this->registerKey, $this->code );
        Redis::expire( $this->registerKey, 1800 );
    }

    private function saveResetCode()
    {
        Redis::set( $this->resetKey, $this->code );
        Redis::expire( $this->resetKey, 1800 );
    }

    public function sendRegisterCode()
    {
        if ( ! $this->isLock )
        {
            $this->createCode();
            $this->saveRegisterCode();
            $this->Response = $this->aliyunSMS(array(
                    'code'          => $this->code,
                    'templateCode'  => $this->RegisterTemplateCode,
            ));
            $this->Response = (array)json_decode($this->Response);
            if ( $this->Response['success'] == true )
            {
                $this->addLock();
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            $this->error = 0;
            return false;
        }
    }

    public function sendResetCode()
    {

    }

    private function aliyunSMS(array $params = [])
    {
        $method = 'GET';
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $this->APPCODE);
//        $querys = 'ParamString={"code":"123456"}&RecNum=' . $this->phone . '&SignName=英语学习&TemplateCode=SMS_100880029';

        $querys = 'ParamString={"code":"' . $params['code'] . '"}';
        $querys .= '&RecNum=' . $this->phone;
        $querys .= '&SignName=' . $this->SignName;
        $querys .= '&TemplateCode=' . $params['templateCode'];

        $url = $this->host . $this->api . '?' . $querys;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
        if (1 == strpos("$".$this->host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $response = curl_exec($curl);
        curl_close( $curl );
        return $response;
    }
}